﻿using System;
using CoreTypes;
using System.Custom;
using System.Drawing;
using System.Reflection;
using System.Custom.Window;
using System.Globalization;
using System.Windows.Forms;
using System.ComponentModel;
using MainModule.Properties;

namespace MainModule
{
   partial class AboutBox : Window
   {
      InternetCommunicator FCommunicator = InternetCommunicator.Self;

      public AboutBox()
      {
         InitializeComponent();

         labelUpdates.Text = "";
         
         Icon = Icon.FromHandle(ResourceWrapper.GetImage("CitronIcon_16").GetHicon());

         FCommunicator.PropertyChanged += communicator_PropertyChanged;
         FCommunicator.InstallationCheck += communicator_CheckingUpdates;

         UpdatewWindowView();
      }
      protected override void Dispose(bool disposing)
      {
         FCommunicator.PropertyChanged -= communicator_PropertyChanged;
         FCommunicator.InstallationCheck -= communicator_CheckingUpdates;

         if (disposing && (components != null))
         {
            components.Dispose();
         }
         base.Dispose(disposing);
      }

      protected override void UpdatewWindowView()
      {
         labelCopyright.Text = InternetCommunicator.AppInfo.LegalCopyright;
         Text = Resources.S_About + " " + InternetCommunicator.AppInfo.ProductName;
         linkLabelWebsite.Text = Settings.Default.BaseUrl.SubstrAfter("//");
         labelVersion.Text = InternetCommunicator.VersionText;
      }

      private void window_Shown(object sender, EventArgs e)
      {
         DelayedAction.Do(() => { FCommunicator.CheckInstallation(CheckInstallationArg.Update, ""); }, 5000);
      }
      private void window_Paint(object sender, PaintEventArgs e)
      {
         FCommunicator.DrawTheme(FCommunicator.ColorTheme, e.Graphics, (sender as Control).ClientRectangle, ColorThemeObject.ToolStrip);
      }
      private void window_KeyDown(object sender, KeyEventArgs e)
      {
         if (e.KeyCode == Keys.Escape) Close();
      }
      private void pictureBoxLogo_Click(object sender, EventArgs e)
      {
         Close();
      }
      private void linkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
      {
         Close();

         if (e.Button == MouseButtons.Left)
         {
            if (sender == labelCopyright)
            {
               System.Diagnostics.Process.Start(Settings.Default.BaseUrl + Settings.Default.UrlLicense);
            }
            else if (sender == labelVersion)
            {
               System.Diagnostics.Process.Start(Settings.Default.BaseUrl + Settings.Default.UrlVersionHistory);
            }
            else
            {
               System.Diagnostics.Process.Start(Settings.Default.BaseUrl);
            }
         }
      }
      private void communicator_CheckingUpdates(object sender, InstallationCheckEventArgs e)
      {
         switch (e.State)
         {
            case InstallationState.CheckInProgress:
            {
               labelUpdates.Text = Resources.S_Checking_updates;
               break;
            }
            case InstallationState.NoUpdates:
            {
               labelUpdates.Text = Resources.S_You_are_using_the_latest_version;
               break;
            }
            case InstallationState.InstallReady:
            {
               labelUpdates.Text = Resources.S_Updates_will_be_installed_automatically;
               break;
            }
            case InstallationState.ForcedInstall:
            {
               FCommunicator.Shutdown(true);
               Application.Restart();
               break;
            }
            default:
            {
               labelUpdates.Text = Resources.S_Updates_are_unavailable;
               break;
            }
         }
      }
      private void communicator_PropertyChanged(object sender, PropertyChangedEventArgs e)
      {
         if (e.PropertyName == "ColorTheme") ColorTheme = FCommunicator.ColorTheme;
         else if (e.PropertyName == "Culture") Translate(FCommunicator.Culture);
      }
   }
}
